#include "widget.h"
#include "ui_widget.h"
#include <QTextCodec>
#include <QIcon>
#include <QSize>
#include <QModelIndex>
#include <QVariant>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QPixmap>
#include <QDebug>
#include "SerialEvent.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFocus();
    // 定义文本编码
    tc = QTextCodec::codecForName("UTF-8");
    // 实例化串口类
    serial_main = new Serial();
    // 初始化波特率列表
    baudRateList << "4800" << "9600" << "19200" << "38400" << "57600" << "74800" << "115200";
    // 初始化数据位列表
    dataBitsList << "5" << "6" << "7" << "8";
    // 初始化校验位列表
    checkBitsList << "无校验" << "奇校验" << "偶校验";
    // 初始化停止位列表
    stopBitsList << "1" << "1.5" << "2";
    // 初始化发送后缀列表
    suffixList << "无追加" << "\\r" << "\\n" << "\\r\\n" << "\\n\\r";
    ui->serial_baud_C->addItems(baudRateList);
    ui->serial_baud_C->setCurrentIndex(6); // 默认显示第七个元素
    ui->serial_check_C->addItems(checkBitsList);
    ui->serial_data_C->addItems(dataBitsList);
    ui->serial_data_C->setCurrentIndex(3); // 默认显示第四个元素
    ui->serial_stop_C->addItems(stopBitsList);
    ui->transmit_suffix->addItems(suffixList);
    /*****************edit 22.04.12*******************/
    // 富文本不开启
    ui->text_receive->setAcceptRichText(false);
    ui->text_transmit->setAcceptRichText(false);
    ui->dataTemp->setAcceptRichText(false);
    ui->deleteData->setAcceptRichText(false);
    ui->replaceData->setAcceptRichText(false);
    /*************************************************/
    this->siderBarConfig();
    // 样式表
    this->setStyleSheet(//正常状态样式
                        "#serial_ONOFF_P,#save_file,#receive_clear,#serial_updata,#transmit_data,#deleteBtn,#changeBtn,#findBtn,#deleteBtn2,#findBtn2{"
                        "background-color:rgb(255,255,255);" // 背景色（也可以设置图片）
                        "border-style:outset;" // 边框样式（inset/outset）
                        "border-radius:8px;"
                        "border-color:rgba(71,75,76,100);" // 边框颜色
                        "font: 16px;" // 字体，字体大小
                        "font-weight:bold;" // 字体粗细
                        "color:rgba(255, 103, 76,255);" // 字体颜色
                        "padding:1px;" // 填衬
                        "}"
                        //鼠标悬停样式
                        "#serial_ONOFF_P:hover,#save_file:hover,#receive_clear:hover,#serial_updata:hover,#transmit_data:hover,#changeBtn:hover,#deleteBtn:hover,#findBtn:hover,#deleteBtn2:hover,#findBtn2:hover{"
                        "background-color:rgba(255, 103, 76,255);"
                        "border-color:rgba(255,255,255,200);"
                        "font:bold 17px;"
                        "color:rgba(255,255,255,255);"
                        "}"
                        /**********************edit 220426***********************/
                        // 设置背景图
                        "#tab1_USART{"
                        "border-image: url(:/IMG/backimg.jpg);"
                        "}"
                        // 设置背景色 半透明
                        "#text_receive,#text_transmit{"
                        "background-color:rgba(255,255,255,0.7);"
                        //边框粗细-颜色-圆角设置
                        "border-radius:15px;"
                        "}"
                        // 设置背景图
                        "#tab3_data{"
                        "border-image: url(:/IMG/backimg2.jpg);"
                        "}"
                        // 设置背景色 半透明
                        "#dataTemp,#deleteData,#replaceData{"
                        "background-color:rgba(0,0,0,0.6);"
                        "border-radius:15px;"
                        "}"
                        );
    // 初始化时串口扫描一次
    serialNameList = serial_main->serialScan();
    ui->serial_port_C->addItems(serialNameList);
    // 串口接收数据并显示
    connect(serial_main, &Serial::readSignal, this, &Widget::receiveDate);
    // 定时发送
    t1 = new QTimer(this);
    connect(t1, &QTimer::timeout, this, &Widget::on_transmit_data_clicked);

    /******************************edit 220427*************************************/
    movie1 = new QMovie("./movies/rotateEarth.gif");
    ui->movie_lab->setMovie(movie1);
    ui->movie_lab->setScaledContents(true);  // 设置缩放为自适应缩放
    mySerialEvent = new SerialEvent();
    // 串口插入处理
    connect(mySerialEvent, &SerialEvent::comDevArriaval, this , [=](){
        ui->serial_port_C->clear();
        serialNameList = serial_main->serialScan();
        ui->serial_port_C->addItems(serialNameList);
    });
    // 串口拔出处理
    connect(mySerialEvent, &SerialEvent::comDevRemoveComplete, this , [=](QString devName){
        // 打开串口时串口变化 如果处于打开状态的端口被拔出则重置串口状态
        if(!serial_ONOFF_P_flag)
        {
            if(ui->serial_port_C->currentText() == devName)
            {
                ui->serial_ONOFF_P->setText("打开串口");
                ui->serial_ONOFF_L->setPixmap(QPixmap(":/IMG/OFF.png"));
                serial_ONOFF_P_flag = true;
                serial_main->close();
                this->setComboboxStatus(false);
                QMessageBox::warning(this, "串口连接错误", "<font size=\"4\">当前串口已关闭</font>");
            }
        }
        ui->serial_port_C->clear(); // 清空串口复选框
        serialNameList = serial_main->serialScan(); // 扫描串口
        ui->serial_port_C->addItems(serialNameList); // 添加串口名
    });
}

void Widget::receiveDate(void)
{
    // ASCII显示是否选中
    if(ui->receive_ASCII->checkState() == Qt::Checked) {
        // 停止显示是否选中
        if(ui->receive_stop->checkState() == Qt::Unchecked) {
            // 日志模式是否选中
            if(ui->receive_diary->checkState() == Qt::Checked) {
                // 数据缓存区是否为空
                if(serial_main->dataBuf.isNull() == false) {
                    // 获取当前时间
                    this->getCurrentTime();
                    dateBufTemp += currentTimer;
                    // QByteArray转QString
                    dateBufTemp += tc->toUnicode(serial_main->dataBuf) + "\n";  // 转中文
                    ui->text_receive->insertPlainText(dateBufTemp); // 修改为光标处追加文本
                    dateBufTemp.clear(); // 清除暂存区防止卡死
                }
            } else {
                dateBufTemp += tc->toUnicode(serial_main->dataBuf);  // 转中文
                ui->text_receive->insertPlainText(dateBufTemp);
                dateBufTemp.clear();
            }
        }
    } else if(ui->receive_HEX->checkState() == Qt::Checked) {
        if(ui->receive_stop->checkState() == Qt::Unchecked) {
            if(ui->receive_diary->checkState() == Qt::Checked) {
                if(serial_main->dataBuf.isNull() == false) {
                    // 获取当前时间
                    this->getCurrentTime();
                    dateBufTemp += currentTimer;
                    // 转大写16进制，空格隔开
                    dateBufTemp += QString(serial_main->dataBuf.toHex(' ')).toUpper() + "\n";
                    ui->text_receive->insertPlainText(dateBufTemp);
                    dateBufTemp.clear();
                }
            } else {
                // 转大写16进制，空格隔开
                dateBufTemp += QString(serial_main->dataBuf.toHex(' ')).toUpper() + " ";
                ui->text_receive->insertPlainText(dateBufTemp);
                dateBufTemp.clear();
            }
        }
    }
}
// 数据发送函数
void Widget::transmitDate(QString transmitData)
{
    QByteArray transmitDataTemp;
    QString suffixTemp;
    suffixTemp = this->suffixHandle(ui->transmit_suffix->currentIndex());
    if(ui->transmit_ASCII->checkState() == Qt::Checked) {
        // 将获取的文本内容转换为QByteArray类型
        transmitDataTemp = tc->fromUnicode(transmitData + suffixTemp);
        serial_main->sendData(transmitDataTemp);
    } else if(ui->transmit_HEX->checkState() == Qt::Checked) {
        // 将获取的文本内容转换为QByteArray类型后再转换为16进制并大写
        transmitDataTemp = QByteArray::fromHex(tc->fromUnicode(transmitData + suffixTemp)).toUpper();
        serial_main->sendData(transmitDataTemp);
    }
}

void Widget::getCurrentTime(void)
{
    // 系统当前时间
    currentTimer = QDateTime::currentDateTime().toString("hh:mm:ss.zzz >> ");
}

QString Widget::suffixHandle(int index)
{
    QString suffixTemp;
    switch(index) {
        case 0: suffixTemp = ""; break;
        case 1: suffixTemp = "\r"; break;
        case 2: suffixTemp = "\n"; break;
        case 3: suffixTemp = "\r\n"; break;
        case 4: suffixTemp = "\n\r"; break;
    }
    return suffixTemp;
}
// true 禁止点击  false开启点击
void Widget::setComboboxStatus(bool status)
{
    ui->serial_port_C->setAttribute(Qt::WA_TransparentForMouseEvents, status);
    ui->serial_baud_C->setAttribute(Qt::WA_TransparentForMouseEvents, status);
    ui->serial_data_C->setAttribute(Qt::WA_TransparentForMouseEvents, status);
    ui->serial_check_C->setAttribute(Qt::WA_TransparentForMouseEvents, status);
    ui->serial_stop_C->setAttribute(Qt::WA_TransparentForMouseEvents, status);
    ui->serial_updata->setAttribute(Qt::WA_TransparentForMouseEvents, status);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_text_receive_textChanged()
{
    // 将光标焦点移动至文末为了显示最新的内容
    QTextCursor cursor = ui->text_receive->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->text_receive->setTextCursor(cursor);
}

void Widget::on_serial_ONOFF_P_clicked()
{
    // 判断串口状态标志
    if(serial_ONOFF_P_flag) {
        // 判断是否能够打开串口 打不开则报错
        if(serial_main->serialOpen(ui->serial_port_C->currentText(), ui->serial_baud_C->currentText().toInt()))
        {
            currentPort = ui->serial_port_C->currentText();
            ui->serial_ONOFF_P->setText("关闭串口");
            ui->serial_ONOFF_L->setPixmap(QPixmap(":/IMG/ON.png"));
            serial_ONOFF_P_flag = false;
            this->setComboboxStatus(true);
        } else {
            QMessageBox::warning(this, "串口打开错误", "<font size=\"4\">未选择串口或当前串口无法打开</font>");
        }
    } else if(!serial_ONOFF_P_flag) {
        ui->serial_ONOFF_P->setText("打开串口");
        ui->serial_ONOFF_L->setPixmap(QPixmap(":/IMG/OFF.png"));
        // 关闭串口
        serial_main->serialClose();
        serial_ONOFF_P_flag = true;
        this->setComboboxStatus(false);
    }
}

void Widget::on_receive_ASCII_clicked()
{
    if(ui->receive_HEX->checkState() == Qt::Checked) {
        ui->receive_HEX->setCheckState(Qt::Unchecked);
        ui->receive_ASCII->setCheckState(Qt::Checked);
    } else {
        ui->receive_ASCII->setCheckState(Qt::Checked);
    }
}

void Widget::on_receive_HEX_clicked()
{
    if(ui->receive_ASCII->checkState() == Qt::Checked) {
        ui->receive_ASCII->setCheckState(Qt::Unchecked);
        ui->receive_HEX->setCheckState(Qt::Checked);
    } else {
        ui->receive_HEX->setCheckState(Qt::Checked);
    }
}

void Widget::on_receive_clear_clicked()
{
    dateBufTemp = "";
    ui->text_receive->clear();
}

void Widget::on_transmit_auto_clicked()
{
    if(ui->transmit_auto->checkState() == Qt::Checked) {
        // 将键入框内的数据给定时器
        t1->start(ui->transmitTime->text().toInt());
    } else if(ui->transmit_auto->checkState() == Qt::Unchecked) {
        t1->stop();
    }
}

void Widget::on_transmit_ASCII_clicked()
{
    if(ui->transmit_HEX->checkState() == Qt::Checked) {
        ui->transmit_HEX->setCheckState(Qt::Unchecked);
        ui->transmit_ASCII->setCheckState(Qt::Checked);
    } else {
        ui->transmit_ASCII->setCheckState(Qt::Checked);
    }
}

void Widget::on_transmit_HEX_clicked()
{
    if(ui->transmit_ASCII->checkState() == Qt::Checked) {
        ui->transmit_ASCII->setCheckState(Qt::Unchecked);
        ui->transmit_HEX->setCheckState(Qt::Checked);
    } else {
        ui->transmit_HEX->setCheckState(Qt::Checked);
    }
}
// 有问题
void Widget::on_serial_updata_clicked()
{
    serialNameList.clear();
    serialNameList = serial_main->serialScan();
    ui->serial_port_C->clear();
    ui->serial_port_C->addItems(serialNameList);
}

void Widget::on_transmit_data_clicked()
{
    QString dataTemp;
    dataTemp = ui->text_transmit->toPlainText();
    this->transmitDate(dataTemp);
}

void Widget::on_save_file_clicked()
{
    QString filename = QFileDialog::getSaveFileName(this,tr("保存文件"),"",tr("*.txt;;")); //选择路径
    if(filename.isEmpty())
    {
        return;
    }
    else
    {
        QFile *file = new QFile(filename);//此路径下没有就会自己创建一个
        while(!file->open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
           QMessageBox::warning(this,"文件错误","<font size=\"4\">文件保存出错!</font>");
        }
        QTextStream in(file);
        in << dateBufTemp;
        file->close();
    }
}

void Widget::resizeEvent(QResizeEvent *event)
{
    (void)event;
    side_x = this->width() / 3 * 2 - ui->side->width() - 12 - 38;
    side_width = this->width() / 3 + 38;
    side_height = ui->right_USART->height() - ui->bottom_USART->height() - 4;
    sideWidget->setGeometry(side_x, side_y, side_width, side_height);
}

void Widget::on_side_btn_clicked()
{
    if(!sideWidgetFlag) {
        side_x = this->width() / 3 * 2 - ui->side->width() - 12 - 38;
        side_width = this->width() / 3 + 38;
        side_height = ui->right_USART->height() - ui->bottom_USART->height() - 4;
        sideWidget->setGeometry(side_x, side_y, side_width, side_height);
        sideWidget->show();
        sideWidgetFlag = true;
        ui->side_btn->setIcon(QIcon(QPixmap(":/IMG/side_r.png")));
    } else {
        sideWidget->hide();
        sideWidgetFlag = false;
        ui->side_btn->setIcon(QIcon(QPixmap(":/IMG/side_l.png")));
    }
}
/**************************edit 220427********************************/
void Widget::on_serial_port_C_currentIndexChanged(int index)
{
    if(index >= 0)
    {
        // 当串口号复选框内容变化时更新串口描述
        ui->serial_Description->setText(serial_main->serialDescription.at(index));
    }
    else
    {
        // 小于0则复选框为空不做处理
        return ;
    }
}
/******************************************************************/
void Widget::siderBarConfig(void)
{
    sideWidget = new QWidget(this);
    sideWidget->hide();
    QPalette pal(sideWidget->palette());
    pal.setColor(QPalette::Base, Qt::white);
    sideWidget->setAutoFillBackground(true);
    sideWidget->setPalette(pal);
    lineEdit1 = new QLineEdit("AT", sideWidget);
    lineEdit1->setReadOnly(true);
    lineEdit2 = new QLineEdit("AT+RST", sideWidget);
    lineEdit2->setReadOnly(true);
    lineEdit3 = new QLineEdit("AT+GMR", sideWidget);
    lineEdit3->setReadOnly(true);
    lineEdit4 = new QLineEdit("AT+CWMODE", sideWidget);
    lineEdit4->setReadOnly(true);
    lineEdit5 = new QLineEdit("AT+CWJAP", sideWidget);
    lineEdit5->setReadOnly(true);
    lineEdit6 = new QLineEdit("AT+CWLAP", sideWidget);
    lineEdit6->setReadOnly(true);
    lineEdit7 = new QLineEdit("AT+CWQAP", sideWidget);
    lineEdit7->setReadOnly(true);
    lineEdit8 = new QLineEdit("AT+CWSAP", sideWidget);
    lineEdit8->setReadOnly(true);
    lineEdit9 = new QLineEdit("AT+CWLIF", sideWidget);
    lineEdit9->setReadOnly(true);
    lineEdit10 = new QLineEdit("AT+CIPSTATUS", sideWidget);
    lineEdit10->setReadOnly(true);
    lineEdit11 = new QLineEdit("AT+CIPSTART", sideWidget);
    lineEdit11->setReadOnly(true);
    lineEdit12 = new QLineEdit("AT+CIPSEND", sideWidget);
    lineEdit12->setReadOnly(true);
    lineEdit13 = new QLineEdit("AT+CIPCLOSE", sideWidget);
    lineEdit13->setReadOnly(true);
    lineEdit14 = new QLineEdit("AT+CIFSR", sideWidget);
    lineEdit14->setReadOnly(true);
    lineEdit15 = new QLineEdit("AT+CIPMUX", sideWidget);
    lineEdit15->setReadOnly(true);
    lineEdit16 = new QLineEdit("AT+CIPSERVER", sideWidget);
    lineEdit16->setReadOnly(true);
    lineEdit17 = new QLineEdit("AT+CIPMODE", sideWidget);
    lineEdit17->setReadOnly(true);
    lineEdit18 = new QLineEdit("AT+CIPSTO", sideWidget);
    lineEdit18->setReadOnly(true);

    labelTittle1 = new QLabel("WIFI模块AT指令集", sideWidget);
    labelTittle2 = new QLabel("记得勾选追加/r/n",sideWidget);
    label1 = new QLabel("测试AT启动", sideWidget);
    label2 = new QLabel("重启模块", sideWidget);
    label3 = new QLabel("查看版本信息", sideWidget);
    label4 = new QLabel("选择WIFI应用模式", sideWidget);
    label5 = new QLabel("加入AP", sideWidget);
    label6 = new QLabel("列出当前可用AP", sideWidget);
    label7 = new QLabel("退出与AP的连接", sideWidget);
    label8 = new QLabel("设置AP模式下的参数", sideWidget);
    label9 = new QLabel("查看已接入设备的IP", sideWidget);
    label10 = new QLabel("获得连接状态", sideWidget);
    label11 = new QLabel("建立TCP连接/注册UDP", sideWidget);
    label12 = new QLabel("发送数据", sideWidget);
    label13 = new QLabel("关闭TCP或UDP", sideWidget);
    label14 = new QLabel("获取本地IP地址", sideWidget);
    label15 = new QLabel("启动多连接", sideWidget);
    label16 = new QLabel("配置为服务器", sideWidget);
    label17 = new QLabel("设置模块传输模式", sideWidget);
    label18 = new QLabel("设置服务器超时时间", sideWidget);

    int row = 1;
    layout = new QGridLayout;
    layout->addWidget(labelTittle1,row++,1);
    layout->addWidget(lineEdit1,row++,1);
    layout->addWidget(lineEdit2,row++,1);
    layout->addWidget(lineEdit3,row++,1);
    layout->addWidget(lineEdit4,row++,1);
    layout->addWidget(lineEdit5,row++,1);
    layout->addWidget(lineEdit6,row++,1);
    layout->addWidget(lineEdit7,row++,1);
    layout->addWidget(lineEdit8,row++,1);
    layout->addWidget(lineEdit9,row++,1);
    layout->addWidget(lineEdit10,row++,1);
    layout->addWidget(lineEdit11,row++,1);
    layout->addWidget(lineEdit12,row++,1);
    layout->addWidget(lineEdit13,row++,1);
    layout->addWidget(lineEdit14,row++,1);
    layout->addWidget(lineEdit15,row++,1);
    layout->addWidget(lineEdit16,row++,1);
    layout->addWidget(lineEdit17,row++,1);
    layout->addWidget(lineEdit18,row++,1);
    row = 1;
    layout->addWidget(labelTittle2,row++,2);
    layout->addWidget(label1,row++,2);
    layout->addWidget(label2,row++,2);
    layout->addWidget(label3,row++,2);
    layout->addWidget(label4,row++,2);
    layout->addWidget(label5,row++,2);
    layout->addWidget(label6,row++,2);
    layout->addWidget(label7,row++,2);
    layout->addWidget(label8,row++,2);
    layout->addWidget(label9,row++,2);
    layout->addWidget(label10,row++,2);
    layout->addWidget(label11,row++,2);
    layout->addWidget(label12,row++,2);
    layout->addWidget(label13,row++,2);
    layout->addWidget(label14,row++,2);
    layout->addWidget(label15,row++,2);
    layout->addWidget(label16,row++,2);
    layout->addWidget(label17,row++,2);
    layout->addWidget(label18,row++,2);
    sideWidget->setLayout(layout);
}
/*******************edit 22.04.12***********************/
void Widget::on_deleteBtn_clicked()
{
    QString originText = ui->dataTemp->toPlainText();
    QString searchText = ui->deleteData->toPlainText(); // 要替换的数据
    QString replaceText = ui->replaceData->toPlainText(); // 替换为的数据
    QString resultText = originText.replace(searchText, "<font color=\"#ff0000\">" + replaceText + "</font>"); // 替换
    resultText = resultText.replace("\n", "<br>"); // /n在html标签内不换行，故换成html的换行标签解决此问题
    resultText += " "; // 解决文本尾部变色后仍有一个新增字符会与其同色的bug
    ui->dataTemp->setText(resultText);
}

void Widget::on_dataTemp_textChanged()
{
    ui->dataTemp->setTextColor(QColor(0,255,50)); // 解决文本尾部变色后新增文本与其同色的bug
    int charSize1 = ui->dataTemp->toPlainText().length(); // 英文字节数
    int charSize2 = ui->dataTemp->toPlainText().toLocal8Bit().length(); // 中文字节数
    ui->charNum1->setText(QString::number(charSize1));
    ui->charNum2->setText(QString::number(charSize2));
}

void Widget::on_changeBtn_clicked()
{
    QByteArray dataTempToByteArray;
    QString dataTemp;
    switch(ui->codeChoose->currentIndex())
    {
        case 0: // HEX
            dataTempToByteArray = tc->fromUnicode(ui->dataTemp->toPlainText());
            dataTemp = dataTempToByteArray.toHex(' ').toUpper() + " ";
            ui->dataTemp->setPlainText(dataTemp); // 纯文本显示 防止html格式数据被识别
            break;
        case 1: // UTF8
            dataTempToByteArray = QByteArray::fromHex(tc->fromUnicode(ui->dataTemp->toPlainText()).toUpper());
            dataTemp = tc->toUnicode(dataTempToByteArray).toUtf8();
            ui->dataTemp->setPlainText(dataTemp); // 纯文本显示 防止html格式数据被识别
            break;
    }
}
/*******************edit 22.04.13*********************/
void Widget::on_findBtn_clicked()
{
    // 查找数据变色原理是将其添加html标签设置颜色
    /***********************方法一**************************/
    QString originText = ui->dataTemp->toPlainText();
    QString searchText = ui->deleteData->toPlainText(); // 要替换的数据
    QString replaceText = "<font color=\"#ffff00\">" + searchText + "</font>"; // 替换为的数据
    QString resultText = originText.replace(searchText, replaceText); // 替换
    resultText = resultText.replace("\n", "<br>"); // /n在html标签内不换行，故换成html的换行标签解决此问题
    resultText += " "; // 解决文本尾部变色后仍有一个新增字符会与其同色的bug
    ui->dataTemp->setText(resultText);
    /***********************方法二**************************/
//    QStringList originList = ui->dataTemp->toPlainText().split("\n"); // 将每行数据拆分放入QStringList
//    QString searchText = ui->deleteData->toPlainText(); // 要替换的数据
//    originList.replaceInStrings(searchText, "<font color=\"#ff0000\">" + searchText + "</font>"); // 替换
//    QString resultText = originList.join("<br>"); // /n由于未知原因不换行，故换成html的换行标签解决此问题
//    ui->dataTemp->setText(resultText);
}
/*******************edit 22.04.16*********************/
void Widget::on_deleteBtn2_clicked()
{
    if(ui->deleteData->toPlainText().contains("---",Qt::CaseInsensitive))
    {
        QStringList deleteData = ui->deleteData->toPlainText().split("---"); // 按照x---y格式将数据分割
        QString resultText = ui->dataTemp->toPlainText().replace(QString(deleteData.at(1)), QString(deleteData.at(0))); // 把目标数据中的y替换为x
        QStringList resultList = resultText.split(QString(deleteData.at(0))); // 以x分割数据
        QString replaceText = ui->replaceData->toPlainText(); // 替换为的数据
        resultText.clear();
        // 遍历字符串列表
        for(int i = 0; i < resultList.count(); i++)
        {
            // 将偶数数据组合(含0)
            if(i % 2 == 0)
            {
                resultText += resultList.at(i);
            } // 将奇数数据替换
            else
            {
                resultText += "<font color=\"#ff0000\">" + replaceText + "</font>";
            }
        }
        resultText = resultText.replace("\n", "<br>"); // /n在html标签内不换行，故换成html的换行标签解决此问题
        ui->dataTemp->setText(resultText);
    }
    else
    {
        QMessageBox::warning(this,"数据格式错误","<font size=\"4\">请按照x---y的格式<br>例如:<br>目标数据:123(xx)456<br>需要删除(xx)<br>则输入(---)</font>");
    }
}

void Widget::on_dataTitle1_clicked()
{
    QMessageBox::about(this, "操作数据一提示", "<font size=\"4\">此处填写需要替换或查找的数据(替换为空即删除数据)<br>执行范围替换的填写格式请点击[范围替换]按钮查看</font>");
}

void Widget::on_dataTitle2_clicked()
{
    QMessageBox::about(this, "操作数据二提示", "<font size=\"4\">此处填写需要替换为的数据</font>");
}

void Widget::on_dataTitle0_clicked()
{
    QMessageBox::about(this, "目标数据提示", "<font size=\"4\">此处填写需要被操作的目标数据<br>绿色为目标源数据<br>红色是替换的数据<br>黄色是查找的数据</font>");
}

void Widget::on_findBtn2_clicked()
{
    if(ui->deleteData->toPlainText().contains("---",Qt::CaseInsensitive))
    {
        QStringList deleteData = ui->deleteData->toPlainText().split("---"); // 按照x---y格式将数据分割
        QString resultText = ui->dataTemp->toPlainText().replace(QString(deleteData.at(1)), QString(deleteData.at(0))); // 把目标数据中的y替换为x
        QStringList resultList = resultText.split(QString(deleteData.at(0))); // 以x分割数据
        resultText.clear();
        // 遍历字符串列表
        for(int i = 0; i < resultList.count(); i++)
        {
            // 将偶数数据组合(含0)
            if(i % 2 == 0)
            {
                resultText += resultList.at(i);
            } // 将奇数数据与x y重新组合并添加html标签变色
            else
            {
                resultText += "<font color=\"#ffff00\">" + QString(deleteData.at(0)) + resultList.at(i) + QString(deleteData.at(1)) + "</font>";
            }
        }
        resultText = resultText.replace("\n", "<br>"); // /n在html标签内不换行，故换成html的换行标签解决此问题
        ui->dataTemp->setText(resultText);
    }
    else
    {
        QMessageBox::warning(this,"数据格式错误","<font size=\"4\">请按照x---y的格式<br>例如:<br>目标数据:456#xx#123<br>需要查找#xx#<br>则输入#---#</font>");
    }
}
/************************edit 220427****************************/
void Widget::on_tabWidget_currentChanged(int index)
{
    if(index == 3) movie1->start();
}

